home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / pyshared / gst-0.10 / gst / extend / pygobject.py < prev    next >
Encoding:
Python Source  |  2009-02-21  |  5.4 KB  |  173 lines

  1. #!/usr/bin/env python
  2. # -*- Mode: Python -*-
  3. # vi:si:et:sw=4:sts=4:ts=4
  4. #
  5. # GStreamer python bindings
  6. # Copyright (C) 2004 Johan Dahlin <johan at gnome dot org>
  7.  
  8. # This library is free software; you can redistribute it and/or
  9. # modify it under the terms of the GNU Lesser General Public
  10. # License as published by the Free Software Foundation; either
  11. # version 2.1 of the License, or (at your option) any later version.
  12. # This library is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15. # Lesser General Public License for more details.
  16. # You should have received a copy of the GNU Lesser General Public
  17. # License along with this library; if not, write to the Free Software
  18. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  19.  
  20. """
  21. PyGTK helper functions
  22. """
  23.  
  24. import sys
  25.  
  26. import gobject
  27.  
  28. def gobject_set_property(object, property, value):
  29.     """
  30.     Set the given property to the given value on the given object.
  31.  
  32.     @type object:   L{gobject.GObject}
  33.     @type property: string
  34.     @param value:   value to set property to
  35.     """
  36.     for pspec in gobject.list_properties(object):
  37.         if pspec.name == property:
  38.             break
  39.     else:
  40.         raise errors.PropertyError(
  41.             "Property '%s' in element '%s' does not exist" % (
  42.                 property, object.get_property('name')))
  43.         
  44.     if pspec.value_type in (gobject.TYPE_INT, gobject.TYPE_UINT,
  45.                             gobject.TYPE_INT64, gobject.TYPE_UINT64):
  46.         try:
  47.             value = int(value)
  48.         except ValueError:
  49.             msg = "Invalid value given for property '%s' in element '%s'" % (
  50.                 property, object.get_property('name'))
  51.             raise errors.PropertyError(msg)
  52.         
  53.     elif pspec.value_type == gobject.TYPE_BOOLEAN:
  54.         if value == 'False':
  55.             value = False
  56.         elif value == 'True':
  57.             value = True
  58.         else:
  59.             value = bool(value)
  60.     elif pspec.value_type in (gobject.TYPE_DOUBLE, gobject.TYPE_FLOAT):
  61.         value = float(value)
  62.     elif pspec.value_type == gobject.TYPE_STRING:
  63.         value = str(value)
  64.     # FIXME: this is superevil ! we really need to find a better way
  65.     # of checking if this property is a param enum  
  66.     # also, we only allow int for now
  67.     elif repr(pspec.__gtype__).startswith("<GType GParamEnum"):
  68.         value = int(value)
  69.     else:
  70.         raise errors.PropertyError('Unknown property type: %s' %
  71.             pspec.value_type)
  72.  
  73.     object.set_property(property, value)
  74.  
  75. def gsignal(name, *args):
  76.     """
  77.     Add a GObject signal to the current object.
  78.     To be used from class definition scope.
  79.  
  80.     @type name: string
  81.     @type args: mixed
  82.     """
  83.     frame = sys._getframe(1)
  84.     _locals = frame.f_locals
  85.     
  86.     if not '__gsignals__' in _locals:
  87.         _dict = _locals['__gsignals__'] = {}
  88.     else:
  89.         _dict = _locals['__gsignals__']
  90.  
  91.     _dict[name] = (gobject.SIGNAL_RUN_FIRST, None, args)
  92.  
  93. PARAM_CONSTRUCT = 1<<9
  94.  
  95. def with_construct_properties(__init__):
  96.     """
  97.     Wrap a class' __init__ method in a procedure that will construct
  98.     gobject properties. This is necessary because pygtk's object
  99.     construction is a bit broken.
  100.  
  101.     Usage::
  102.  
  103.         class Foo(GObject):
  104.             def __init__(self):
  105.                 GObject.__init(self)
  106.             __init__ = with_construct_properties(__init__)
  107.     """
  108.     frame = sys._getframe(1)
  109.     _locals = frame.f_locals
  110.     gproperties = _locals['__gproperties__']
  111.     def hacked_init(self, *args, **kwargs):
  112.         __init__(self, *args, **kwargs)
  113.         self.__gproperty_values = {}
  114.         for p, v in gproperties.items():
  115.             if v[-1] & PARAM_CONSTRUCT:
  116.                 self.set_property(p, v[3])
  117.     return hacked_init
  118.  
  119. def gproperty(type_, name, desc, *args, **kwargs):
  120.     """
  121.     Add a GObject property to the current object.
  122.     To be used from class definition scope.
  123.  
  124.     @type type_: type object
  125.     @type name: string
  126.     @type desc: string
  127.     @type args: mixed
  128.     """
  129.     frame = sys._getframe(1)
  130.     _locals = frame.f_locals
  131.     flags = 0
  132.     
  133.     def _do_get_property(self, prop):
  134.         try:
  135.             return self._gproperty_values[prop.name]
  136.         except (AttributeError, KeyError):
  137.             raise AttributeError('Property was never set', self, prop)
  138.  
  139.     def _do_set_property(self, prop, value):
  140.         if not getattr(self, '_gproperty_values', None):
  141.             self._gproperty_values = {}
  142.         self._gproperty_values[prop.name] = value
  143.     
  144.     _locals['do_get_property'] = _do_get_property
  145.     _locals['do_set_property'] = _do_set_property
  146.     
  147.     if not '__gproperties__' in _locals:
  148.         _dict = _locals['__gproperties__'] = {}
  149.     else:
  150.         _dict = _locals['__gproperties__']
  151.     
  152.     for i in 'readable', 'writable':
  153.         if not i in kwargs:
  154.             kwargs[i] = True
  155.  
  156.     for k, v in kwargs.items():
  157.         if k == 'construct':
  158.             flags |= PARAM_CONSTRUCT
  159.         elif k == 'construct_only':
  160.             flags |= gobject.PARAM_CONSTRUCT_ONLY
  161.         elif k == 'readable':
  162.             flags |= gobject.PARAM_READABLE
  163.         elif k == 'writable':
  164.             flags |= gobject.PARAM_WRITABLE
  165.         elif k == 'lax_validation':
  166.             flags |= gobject.PARAM_LAX_VALIDATION
  167.         else:
  168.             raise Exception('Invalid GObject property flag: %r=%r' % (k, v))
  169.  
  170.     _dict[name] = (type_, name, desc) + args + tuple((flags,))
  171.